<template>
  <lay-container fluid="true" style="padding: 10px;height: 100%;overflow-y: hidden">

    <lay-card>
      <lay-form style="margin-top: 20px" >
        <lay-row>

          <lay-col :md="18">
            <lay-row>
              <lay-col :md="12">
                <lay-form-item label="日期：" label-width="70">
                  <lay-date-picker v-model="qm.dtb" placeholder="开始日期" allowClear simple ></lay-date-picker>
                </lay-form-item>
              </lay-col>
              <lay-col :md="12">
                <lay-form-item label="~" label-width="10">
                  <lay-date-picker v-model="qm.dte" placeholder="截至日期" allowClear simple></lay-date-picker>

                </lay-form-item>
              </lay-col>
            </lay-row>

          </lay-col>


          <lay-col :md="6">
            <lay-form-item label-width="0">

              <lay-button type="primary" @click="toSearch">查询</lay-button>
              <lay-button @click="toReset">重置</lay-button>


            </lay-form-item>
          </lay-col>
        </lay-row>
      </lay-form>
    </lay-card>

    <div class="maintable-box"  >

      <lay-table
          :height="'100%'"
          even
          :page="page"
          :columns="columns"
          :dataSource="dataSource"
          :default-toolbar="defaultToolbar"
          v-model:selectedKey="selectedKey"
          v-model:selectedKeys="selectedKeys"
          :rowClassName="rowClassName"

          @row="rowClick"
          @Change="pageChange"
          @sortChange="sortChange"
      >
        <template v-slot:toolbar>
          <lay-row>
            <lay-col :md="18">

              <!--<lay-button size="sm">删除</lay-button> -->
            </lay-col>
            <lay-col style="text-align:right;">


              <lay-button prefix-icon="layui-icon-export" suffix-icon=""
                          @click="tableExport"
              >导出</lay-button>

            </lay-col>
          </lay-row>

        </template>
        <template v-slot:imgurl="{ data }">
          <lay-icon :type="data.imgurl"></lay-icon>
        </template>

        <template v-slot:operator="{ }">

        </template>

      </lay-table>

    </div>

  </lay-container>
</template>

<script lang="ts">
import { ref, watch,reactive,onMounted,h } from 'vue'
import { layer } from '@layui/layer-vue'
import MyHttp from "../../library/MyHttp";
import MyWin from "../../library/MyWin";
import MyPage from "../../library/MyPage";
import {DateUtil} from "../../library/DateUtil";
import {MySimpleExport,TableColumn} from "../../library/MySimpleExport";



export default {

  props:{
    sid:String 
  },

  setup(prop:any, context:any) {

    const baseUrl="/commdata";
    const baseQUrl=baseUrl+"/page";
    const pageName="通讯数据";

    const myWin=ref<any>(null);
    const selectedKey = ref<any>("")
    const selectedKeys = ref<any>([])
    const defaultToolbar = ref(false)
    const page = ref(  new MyPage()  );

    const columns = [

      // {
      //   title: '选择',
      //   width: '50px',
      //   type: 'radio',
      //   fixed: 'left'
      // },
      {
        title: '序号',
        width: '50px' ,
        key: 'rowNo'
      },
      { key:'crtDt',title: '数据时间', width: '100px', },
      { key:'method',title: '上传下发', width: '60px', },
      { key:'data',title: '原始数据', width: '400px', },
      { key:'memo',title: '备注', width: '150px', },


    ]

    const dataSource = ref([]);

    const rowClick = function (data: any) {
      selectedKey.value=data.id;
    }

    const rowDoubleClick = function (data: any) {}

    const pageChange = function ({ current, limit }: any) {
      if(qm.pageSize!=limit)
      {
        qm.pageSize=limit ;
        qm.page=1;
        query(1);
      }else{
        query(current)
      }


    }
    function toSearch() {
      query(1);
    }

    function toReset() {
      qm.key= '';
      qm.dtb=DateUtil.todayStr();
      qm.dte=DateUtil.todayStr();
    }

    const qm=reactive({
      sid:prop.sid,
      key:"",
      dtb:DateUtil.todayStr(),
      dte:DateUtil.todayStr(),
      page: 1,
      pageSize: 10,
      sortFields:"id desc"
    });
    const query=async (pageIndex:number) =>{




      if(pageIndex){
        page.value.current=pageIndex;
        qm.page=pageIndex;
      }
      if (""==qm.dtb||""==qm.dte){
          MyWin.alert("日期必须输入",{})
        return
      }
      var r=await new MyHttp().page(baseUrl+"/page",qm,
          {
          },
          columns
      );
      dataSource.value=r.data.records;
      page.value.total=r.data.totalRow;
    }


    const sortChange = (key: any, sort: number) => {
      qm.sortFields=`${key} ${sort}`;
      query(0);
    }
    const tableExport=async()=>{

      var cols=columns as Array<TableColumn>;
      new MySimpleExport().exportExcel(
          {
            qm,
            url:baseQUrl,
            name:pageName,
            columns:cols,
            lx:"page"
          }
      );
    }


    const rowClassName=(row:any, rowIndex:number):string=>{
      if(row.id==selectedKey.value){
        return "";        //RowSelected
      }
      else{
        return "";
      }
    }
    const toSubmit=(event:any)=>{
        console.info(event)
    }

    onMounted(() =>{
      myWin.value=new MyWin();
      query(1);
    });
    return {
      columns,
      dataSource,
      selectedKey,
      selectedKeys,
      defaultToolbar,
      page,
      rowClick,
      rowDoubleClick,
      pageChange,
      toReset,
      toSearch,


      qm,
      query,

      sortChange,
      tableExport,

      myWin,
      rowClassName,
      toSubmit,


    }
  }
}
</script>
<style  scoped >

.maintable-box{
  height: calc(100% - 140px);
}

</style>